#ifndef QESPEAK_H
#define QESPEAK_H

#include <QObject>
#include <QMediaPlayer>
extern "C" {
#include <speak_lib.h>

#ifdef __WIN32__
#include <windows.h>
#define sleep(x) Sleep(1000*x)
#endif
}

class QEspeak : public QObject
{
    Q_OBJECT

public:
    QEspeak(const char *_esp_data_path);
    virtual ~QEspeak();
    int setSpeakFile(const QString& filename);

    int setRate(int rate);
    void updateValume(int val);
    int setFormat(int format);

    int setGender(uchar gender);
    int setAge(uchar age);
    int setVariant(uchar var);
    int setLanguage(const QString& language);
    int setVoiceName(const QString& voicename);
    int setVoice(const QString& voice);

    int setSpeakText(const QString& text);
    int setVoiceByProperties();
    int setVoiceByProperties(const QString& voicename, const QString& language, uchar gender, uchar age, uchar variant);
    int setOutputMethod(espeak_AUDIO_OUTPUT output);
    int reInitialize();
    int saveToFile(const QString& filename);
    int speakText(const QString& text);
    int speakFile(const QString& filename);
    size_t setSpeakTextSize(size_t size);
public slots:

    int togglePause();
    int speak();
    int stop();
    int reset();
private:
    static int saveToFileCallback(short* wav, int num, espeak_EVENT *pEv);
    static int directSpeakCallback(short* wav, int num, espeak_EVENT *pEv);
    const espeak_VOICE *searchVoiceSpecByLanguege(const QString &languege);
signals:
    void sendEvent(const espeak_EVENT *event_type);
private:
    /*
    *    m_pMediaPlayer is used to play the .wav file under the Qt.
    */
    QMediaPlayer *m_pMediaPlayer;
    QString m_playFileName;
    //

    static QString m_saveFileName;
    int m_saveFileFlag;
    QString m_toSpeakText;
    const char *m_pPauseSpeakPosition;
    int m_pauseFlag;

    QString m_voiceName;
    QString m_voiceLanguage;
    uchar m_gender, m_age, m_variant;
    int m_setVoiceRet;

    /*
    *   Below parameters are used for espeak_SetParameter function.
    *   Set the value of the para.
    *
    *   espeakRATE:    说话速度，以每分钟的字来表示。值80到450。
    *   espeakVOLUME:  音量范围0-200或更多。
    *                 0=沉默, 100=正常的满音量，较大的值可能产生振幅压缩或失真
    *   espeakPITCH:   基音，范围0-100。50 =正常
    *   espeakRANGE:   螺距范围，范围0-100。0=单调 50=正常
    *
    *   espeakWORDGAP:  单词之间的停顿，单位为10mS(默认速度)
    */
    int m_paraRateValue;            //  值80到450。
    int m_paraValumeValue;      //  音量范围0-200或更多。
    int m_paraPitchValue;       //  基音，范围0-100。50 =正常
    int m_paraRangeValue;       //  螺距范围，范围0-100。0=单调 50=正常
    int m_paraWorkgapValue;   //  单词之间的停顿，单位为10mS(默认速度)
    int m_setParaRet;
    //

    // for espeak_Initialize();
    espeak_AUDIO_OUTPUT m_initOutput;
    int m_initBuflenght;
    char *m_pInitPath;
    int m_initOptions;
    int n_initRet;
    //
    const char *m_pEspeakDataPath;

    //
    espeak_VOICE m_voiceSpec;
    const espeak_VOICE **m_ppVoiceSpecList;
    size_t m_spTextSize;

};

#endif // QESPEAK_H
